function checkRequired(elem){
	if(elem.type=="checkbox"||elem.type=='radio'){
		var num  = getInputsByName(elem.name).numChecked;
		return num;
	}else{
		return elem.value;
	}
}

function getInputsByName(name){
	var results=[];
	results.numChecked=0;
	
	var input  = document.getElementsByTagName('input');
	for(var i=0;i<input.length;i++){
		if(input[i].name==name){
			results.push(input[i]);
			if(input[i].checked){
				results.numChecked++;
			}
		}
	}	
	return results;
}

function checkEmail(elem){
	return !elem.value||/^[a-z0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test(elem.value);
}

function checkUrl(elem){
	return /^https?:\/\/([a-z0-9-]+\.)+[a-z0-9]{2,4}.*$/.test( elem.value );
}

function checkPhone(elem){
	var  m = /(\d{3}).*(\d{3}).*(\d{4})/.exec(elem.value);
	if(m!==null){
		elem.value="("+m[1]+")"+m[2]+"-"+m[3];
		return !elem.value==''||m!=null;
	}
}

function checkDate(elem){
	return /^\d{2}\/\d{2}\/\d{2,4}$/.test(elem.value);
}

var errMsg={
		required:{
			msg:"필수 입력필드 입니다.",
			test:function(obj,load){
				return obj.value;
			}
		},
		email:{
			msg:"email not valid",
			test:function(obj){
				return !obj.value||/^[a-z0-9_+.-]+\@([a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test(obj.value);
			}
		},
		phone:{
			msg:"phone not valid.",
			test:function(obj){
				var  m = /(\d{3}).*(\d{3}).*(\d{4})/.exec(obj.value);
				if(m!==null){
					obj.value="("+m[1]+")"+m[2]+"-"+m[3];
					return !obj.value||m;
				}
			}
		},
		date:{
			msg:"date not valid",
			test:function(obj){
				return !obj.value||/^\d{2}\/\d{2}\/\d{2,4}$/.test(obj.value);
			}
		},
		url:{
			msg:"not  a valid  url",
			test:function(obj){
				return !obj.value||/^https?:\/\/([a-z0-9-]+\.)+[a-z0-9]{2,4}.*$/.test( obj.value );
			}
		}		
};


function  validateForm(form,load){
	var valid=true;
	errNum = 0;
	for(var i=0;i<form.elements.length;i++){
		hideErrors(form.elements[i]);
		if(!validateField(form.elements[i], load)){
			//return  false;
			errNum++;
		}
	}
	return errNum>0?false:true;
}

function validateField(elem,load){
	var errors=[];
	for(var name in errMsg){
		var re =  new RegExp("(^|\\s)" + name + "(\\s|$)");
		if(re.test(elem.className)&& !errMsg[name].test(elem,load)){
			errors.push( errMsg[name].msg );
		}
	}
	
	if(errors.length){
		showErrors(elem,errors);
	}
	return errors.length == 0;
}

function hideErrors(elem){
	var next = elem.nextSibling;
	if(next && next.nodeName=="UL"&&next.className=="errors"){
		elem.parentNode.removeChild(next);
	}
}

function showErrors(elem,errors){
	var next = elem.nextSibling;
	if(next&&(next.nodeName != "UL" || next.className != "errors")){
		next = document.createElement("ul");
		next.className="errors";
		elem.parentNode.insertBefore(next,elem.nextSibling);
	}
	
	for(var i = 0;i<errors.length;i++){
		var  li = document.createElement("li");
		li.innerHTML = errors[i];
		next.appendChild(li);
	}
}

function watchFields(form){
	for(var i=0;i<form.elements.length;i++){
		if(form.elements[i].nodeType==1){
			addEvent(form.elements[i],'blur',function(){
				return validateField(this);
			});
		}
	}
}

